/*
 * @Author: aggfb 714740235@qq.com
 * @Date: 2024-05-18 14:29:52
 * @LastEditors: aggfb 714740235@qq.com
 * @LastEditTime: 2024-06-08 16:23:03
 * @FilePath: \2.code\label_rack_control\firmware\app\app.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
#include "app.h"
#include "main.h"
#include "ulog.h"

static uint8_t system_status = APP_SYSTEM_CONFIG;

extern uint64_t main_get_system_times(void);

/**
 * 获取系统状态
**/
uint8_t app_get_system_status(void)
{
    return system_status;
}

/**
 * 系统状态
**/
void app_set_system_status(uint8_t val)
{
    system_status = val;
}

/**
 * 手动模式处理
**/
static void app_run_manual(void)
{
    motor_ctrl_t *motor_ctrl = motor_ctrl_dev();
    ui_screen_t *ui_dev = ui_screen_dev();

    /* 故障码检测 */
    if(motor_ctrl->fault_type != MOTOR_FAULT_DEF)
    {
        motor_ctrl_stop();
        return;
    }

    /* 有物料时,手动模式不运行 */
    if (BSP_GPIO_GET_SENSER == BIT_RESET) /* 检测到有物料*/
    {
        motor_ctrl_stop();
        return;
    }

    /* 有进料指令 */
    if (motor_ctrl->feed_state == 1)
    {
        /* 按键一直被按下超时 */
        if((main_get_system_times() - ui_dev->key_over_time) > BUTTON_TIMEOUT)
        {
            ui_dev->key_over_time = main_get_system_times();
            motor_ctrl->feed_state = 0;
            motor_ctrl->fault_type = motor_ctrl->fault_type | MOTOR_FAULT_PRESS_OVER_TIME;
        }
        //motor_ctrl_running(2, 1, 50, 2000);
        #if (LOG_OUTPUT_EN == 1)
        SEGGER_RTT_printf(0, "app_run_manual.\r\n");
        #endif
    }
    else
    {
        motor_ctrl_stop();
    }
}

/**
 * 自动模式处理
 **/
static void app_run_automatic(void)
{
    motor_ctrl_t *motor_ctrl = motor_ctrl_dev();
    fiber_optic_t *fiber_optic = fiber_optic_dev();
    ui_screen_t *ui_dev = ui_screen_dev();
    
    /* 故障码检测 */
    if(motor_ctrl->fault_type != MOTOR_FAULT_DEF)
    {
        motor_ctrl_stop();
        return;
    }

    /* 自动模式状态机 */
    switch (system_status)
    {
    case APP_SYSTEM_DEF:
        break;
    case APP_SYSTEM_CONFIG: /* 配置运行参数 */
		motor_ctrl->flag_trig = 0;
        motor_ctrl_running(2, 1, motor_ctrl->speed_ratio_cmd, MOTOR_POSTION_BASE);
        system_status = APP_SYSTEM_RUNNING;
        #if (LOG_OUTPUT_EN == 1)
        SEGGER_RTT_printf(0, "config run info state = %d .\r\n", system_status);
        #endif
        break;
    case APP_SYSTEM_RUNNING: /* 持续运行 */
        if(motor_ctrl->flag_trig == 1) /* 物料检测,触发超时 */
        {
            motor_ctrl->flag_trig = 0;
            motor_ctrl_stop();
            system_status = APP_SYSTEM_OVER_TIME_STOP;
            ui_screen_sned_fault_type(MOTOR_FAULT_SHORTAGE);
            #if (LOG_OUTPUT_EN == 1)
            SEGGER_RTT_printf(0, "material timeout state = %d .\r\n", system_status);
            #endif
        }
        if(fiber_optic->flag_trig == 1) /* 检测到物料标志 */
        {
            fiber_optic->flag_trig = 0;
            system_status = APP_SYSTEM_RUNNING_OFFSET;
            if((motor_ctrl->speed_ratio_cmd != 0) && (motor_ctrl->offset_cmd != 0))
            {
                motor_ctrl_running(2, 1, motor_ctrl->speed_ratio_cmd, motor_ctrl->offset_cmd);
            }
            else
            {
                motor_ctrl->flag_trig = 1;
            }
            #if (LOG_OUTPUT_EN == 1)
            SEGGER_RTT_printf(0, "have go offset = %d .\r\n", system_status);
            #endif
        }
        break;
    case APP_SYSTEM_RUNNING_OFFSET:
            if(motor_ctrl->flag_trig == 1)  /* 执行完成offset标志 */
            {
                motor_ctrl->flag_trig = 0;
                motor_ctrl_stop();
                system_status = APP_SYSTEM_NORMAL_PAUSE; 
                #if (LOG_OUTPUT_EN == 1)
                SEGGER_RTT_printf(0, "normal stop state = %d .\r\n", system_status);
                #endif
            }
        break;
    case APP_SYSTEM_NORMAL_PAUSE: /* 正常停止 */
        if (BSP_GPIO_GET_SENSER == BIT_SET) /* 检测到没有物料*/
        {
            system_status = APP_SYSTEM_CONFIG;
            #if (LOG_OUTPUT_EN == 1)
            SEGGER_RTT_printf(0, "A into config state = %d .\r\n", system_status);
            #endif
        }
        break;
    case APP_SYSTEM_OVER_TIME_STOP: /* 物料检测,超时停止 */     
        motor_ctrl->fault_type = motor_ctrl->fault_type | MOTOR_FAULT_SHORTAGE;
        break; 
    default: break;
    }
}

/**
 * 主处理逻辑
*/
void app_main(void)
{
    motor_ctrl_t* motor_ctrl = motor_ctrl_dev();   

    /* 自动模式 */
    if(motor_ctrl->manual_automatic == MOTOR_CTRL_AUTOMATIC) 
    {
        app_run_automatic();
    }
    /* 手动模式 */
    else 
    {
        app_run_manual();
    }
}
